package com.unieagle.exam.app;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.unieagle.exam.account.AccountManager;
import com.unieagle.exam.utils.NetworkUtils;

public class ExamApplication extends Application{
	public static ExamApplication instance;
	
	public static Context getAppContext(){
		return instance.getApplicationContext();
	}

	private AccountManager account;

	private boolean mUiApp = false;

	@Override
	public void onCreate() {
		super.onCreate();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				if(checkDataBase()) {
					copyDBFile();
				}
			}
		}).start();
		
		UMShareAPI.get(this);

		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
				.threadPriority(Thread.NORM_PRIORITY - 1).threadPoolSize(4)
				.denyCacheImageMultipleSizesInMemory().tasksProcessingOrder(QueueProcessingType.LIFO)
				.diskCacheSize(50 * 1024 * 1024).diskCacheFileCount(100)
				.build();
		ImageLoader.getInstance().init(config);
		instance = this;
		String packageName = getPackageName();
		String processName = getProcessName();
		if (packageName.equals(processName)) {
			mUiApp = true;
			account = new AccountManager();
			updateApp();
		}
	}	

	private void copyDBFile() {
		try {
			InputStream myInput = this.getAssets().open("exam.db");
			String outFileName = this.getCacheDir() + "/exam.db";
			File destinationFile = new File(outFileName);
			destinationFile.getParentFile().mkdirs();
			OutputStream myOutput = new FileOutputStream(destinationFile);
			byte[] buffer = new byte[1024];
			int length;
			while ((length = myInput.read(buffer)) > 0) {
				myOutput.write(buffer, 0, length);
			}
			myOutput.flush();
			myOutput.close();
			myInput.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private boolean checkDataBase(){
		SQLiteDatabase checkDB = null;
		String myPath = this.getCacheDir() + "/exam.db";
		try{            
			checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
		}catch(SQLiteException e){
			e.printStackTrace();
		}
		if(checkDB != null){
			checkDB.close();
			File f = new File(myPath);
			f.delete();
		}
		return true;
	}

	public AccountManager getAccountMnager(){
		return account;
	}

	/**
	 * 获取Context所在进程的名称
	 *
	 * @return
	 */
	public String getProcessName() {
		int pid = android.os.Process.myPid();
		ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager.getRunningAppProcesses()) {
			if (appProcess.pid == pid) {
				return appProcess.processName;
			}
		}
		return null;
	}

	public void updateApp(){
		if(NetworkUtils.isNetworkAvailable(this)) {
			new Updater(getAppContext()).start();
		}
	}
	
	{
        PlatformConfig.setWeixin("wx841a74aac7b957dc", "1a0d130929b5aeeaade223669ef667c4");
        PlatformConfig.setQQZone("1106601284", "KQDUDtkBLlsEPQHu");
    }
}
